﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tsgl
{
    public partial class bookBarcodeEdit : Form
    {
        public bookBarcodeEdit()
        {
            InitializeComponent();
        }

        private void setBookInList(string bibliothecaId)
        {
            bibliothecaId = "";
            DataTable dt;
            if (bibliothecaId.Trim() == "")
            {
                dt = bookClass.getBibliothecaInfo(bibliothecaId);
            }
            else
            {
                dt = bookClass.getBibliothecaInfo(bibliothecaId);
                if (dt.Rows.Count != 0)
                {
                    txtBookName.Text = dt.Rows[0][1].ToString();
                    txtBookName.Text = dt.Rows[0][5].ToString();
                }
                else
                {
                    MessageBox.Show("所输入的书目编号不存在，请重新输入", "提示信息");
                }
            }
            this.dgvBookInList.DataSource = null;
            this.dgvBookInList.DataSource = dt;
            this.dgvBookInList.Refresh();
        }

        private int bookInNums;
        private void setBarcodeList()
        {
            string barcodeStart = bookClass.getBarcodeMax();
            int length0 = barcodeStart.Trim().Length;
            for (int i = 1; i <= bookInNums; i++)
            {

                barcodeStart = Convert.ToString(int.Parse(barcodeStart.Trim()) + 1);
                int length1 = length0 - barcodeStart.Trim().Length;
                if (length1 > 0)
                {
                    for (int j = 1; j <= length1; j++)
                    {
                        barcodeStart = "0";
                    }
                }
                if (i == 1)
                {
                    txtBarcodeStart.Text = barcodeStart;
                }
                else
                {
                    listBox1.Items.Add(barcodeStart);
                }
            }
            if (this.listBox1.Items.Count >= 1)
            {
                this.listBox1.SelectedIndex = 0;
                this.axBarCodeCtrl1.Value = this.listBox1.Items[0];
            }
        }

        private void bookIn()
        {
            DataTable dtTemp = bookClass.createTableTemp();
            string bibliothecaId = this.txtBibliothecaId.Text.Trim();
            string bookState = "在藏";
            string bookInDate = DateTime.Now.ToShortDateString();
            for (int i = 0; i <= this.listBox1.Items.Count - 1; i++)
            {
                string bookBarcode = this.listBox1.Items[i].ToString();
                bookClass.bookAdd(bookBarcode, bibliothecaId, bookState, DateTime.Parse(bookInDate));
                DataRow newRow = dtTemp.NewRow();
                newRow[0] = bookBarcode;
                newRow[1] = bibliothecaId;
                newRow[2] = bookState;
                newRow[3] = bookInDate;
                dtTemp.Rows.Add(newRow);
                dtTemp.AcceptChanges();
            }
            this.dgvBarcodeList.DataSource = dtTemp;
            bookClass.bookTotalNumAdd(bibliothecaId, bookInNums);
        }

        private void bookBarcodeEdit_Load(object sender, EventArgs e)
        {
            btnBarcodeOutput.Enabled = false;
            if (this.txtBibliothecaId.Text.Trim() != "")
            {
                this.txtBookName.Enabled = false;
                this.txtBookNumsIn.Enabled = false;
                this.txtBarcodeStart.Enabled = false;
                setBookInList(txtBibliothecaId.Text.Trim());
            }
            else
            {
                setBookInList(this.txtBibliothecaId.Text.Trim());
                this.txtBookName.Text = "";
                this.txtBookNumsIn.Text = "";
                this.txtBibliothecaId.Focus();
            }
        }

        private void txtBibliothecaId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtBibliothecaId.Text.Trim() != "")
                {
                    setBookInList(this.txtBibliothecaId.Text.Trim());
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            axBarCodeCtrl1.Value = listBox1.SelectedItem;
        }

        private void btnBookIn_Click(object sender, EventArgs e)
        {
            if (this.listBox1.Items.Count > 0)
            {
                bookIn();
                setBookInList(this.txtBibliothecaId.Text.Trim());
                this.btnBarcodeOutput.Enabled = true;
            }
            else
            {
                MessageBox.Show("暂没有需要入库的图书", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtBibliothecaId_TextChanged(object sender, EventArgs e)
        {
            if (this.txtBibliothecaId.Text.Trim() == "")
            {

            }
            try
            {
                int.Parse(this.txtBibliothecaId.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("所输入的书目编号有误，请核对后重新输入！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtBookNumsIn_TextChanged(object sender, EventArgs e)
        {
            if (this.txtBibliothecaId.Text != "")
            {
                bookInNums = int.Parse(this.txtBookNumsIn.Text.Trim());
                this.listBox1.Items.Clear();
                this.dgvBarcodeList.DataSource = "";
                this.dgvBarcodeList.Refresh();
                setBarcodeList();
            }
        }

        private void btnBarcodeOutput_Click(object sender, EventArgs e)
        {
            barcodeOutput bookBarcode = new barcodeOutput(this.txtBarcodeStart.Text.Trim(), this.txtBookNumsIn.Text.Trim());
            bookBarcode.Show();
        }

        private void dgvBookInList_DoubleClick(object sender, EventArgs e)
        {
            this.txtBibliothecaId.Text = label5.Text;
            setBookInList(this.txtBibliothecaId.Text.Trim());
        }
    }
}